#include "aky_program/aky_com_include.h"


#define AKY_UART_1_BUF_MAX 50                      // 串口最大接收个数
static uint8_t aky_uart_1_buf[AKY_UART_1_BUF_MAX]; // 串口缓存
static uint8_t aky_uart_1_buf_2[AKY_UART_1_BUF_MAX]; // 串口缓存
static uint16_t aky_uart_1_read_cnt = 0;           // 一帧数据结束串口接收到的数据个数
static uint8_t aky_uart_1_time_out = 0;            // 超时处理  超时则将处理一帧数据标志位置1
static uint8_t flag_aky_uart_1_get_data = 0;
static uint8_t flag_aky_task_1_decode = 0; // 处理一帧数据标志位 置1代表接收到一帧数据
static uint8_t BLEUart1Data[20];
stru_aky_task_1_t Task1_BP;
uint8_t flag_aky_uart_1_judge = 0;//串口1同步标志位判断
static uint32_t u32_tick_last = 0;//超时处理  

static void aky_task_1_uart_decode(void);
static void aky_uart_1_init(void);
static void aky_uart_1_send_byte(uint8_t data);


void UART1_Handler(void) // uart_1 中断
{
    uint32_t chr;
    if (UART_INTStat(UART1, UART_IT_RX_THR | UART_IT_RX_TOUT))
    {
        while (UART_IsRXFIFOEmpty(UART1) == 0)
        {
            if (UART_ReadByte(UART1, &chr) == 0)
            {
                aky_uart_1_buf[aky_uart_1_read_cnt] = (uint8_t)chr;

                aky_uart_1_read_cnt++;
                if (aky_uart_1_read_cnt > AKY_UART_1_BUF_MAX) // 限制最大接收个数
                {
                    aky_uart_1_read_cnt = AKY_UART_1_BUF_MAX - 1;
                }
                aky_uart_1_time_out = 0; // 超时时间清零
                u32_tick_last = g_u32_tick_cnt;
                flag_aky_uart_1_get_data = 1;
            }
        }

        if (UART_INTStat(UART1, UART_IT_RX_TOUT))
        {
            UART_INTClr(UART1, UART_IT_RX_TOUT);
        }
    }
}


void Timer1Callback(void) // 软件定时器 回调函数
{
    aky_uart_1_time_out++;                                        //
    if (g_u32_tick_cnt - u32_tick_last > 3 && flag_aky_uart_1_get_data == 1) // 超时着代表接收完一帧数据
    {
        u32_tick_last = g_u32_tick_cnt;
        flag_aky_uart_1_get_data = 0;
        flag_aky_task_1_decode = 1; // 处理数据标志位置1
    }
}


static uint8_t *p_aky_uart_1_buf;
uint8_t MEM_FIRST_GAIN_VALUE_1 = 0;
uint8_t MEM_FIRST_GAIN_FLAGE_1 = 0;
static void aky_task_1_uart_decode(void)
{
    uint8_t k, Checksum, len;
    uint8_t index = 0;
    uint16_t read_cnt_temp = 0;

    if (flag_aky_task_1_decode == 1)
    {
        read_cnt_temp = aky_uart_1_read_cnt;
        memset(aky_uart_1_buf_2, 0, AKY_UART_1_BUF_MAX);
        memcpy(aky_uart_1_buf_2, aky_uart_1_buf, read_cnt_temp);
        aky_uart_1_read_cnt = 0;
        flag_aky_task_1_decode = 0;

        #if (AKY_PRINTF_TO_UART3 == 1)
        printf("rx1 decode\n");
        for (uint16_t i = 0; i < read_cnt_temp; i++)
        {
            printf(" %02x", aky_uart_1_buf_2[i]);
        }
        printf("\n");
        #endif

        while (read_cnt_temp > 1)
        {
            read_cnt_temp--;
            p_aky_uart_1_buf = &aky_uart_1_buf_2[index];
            index++;
            if ((p_aky_uart_1_buf[0] == 0xA5) && (p_aky_uart_1_buf[1] == 0xA5))
            {
                len = p_aky_uart_1_buf[2];
                if (p_aky_uart_1_buf[len + 3] == 0x5A)
                {
                    Checksum = 0;
                    for (k = 3; k < len + 2; k++)
                    {
                        Checksum += p_aky_uart_1_buf[k];
                    }
                    if (Checksum == p_aky_uart_1_buf[len + 2])
                    {
                        if (p_aky_uart_1_buf[3] == 0x9D)
                        {
                            printf("2222222222!!!!!!\n");
                            aky_task_1_send_cmd(TASK1_BP_READY_Command);

                        }
                        if (p_aky_uart_1_buf[3] == 0x88)
                        {
                            Task1_BP.flag_reply = 1;
                        }

                        if (p_aky_uart_1_buf[3] == 0xC9)
                        {
                            Task1_BP.e_aky_task_1_stat = TASK_1_STAT_PER_WAIT;
                        }
                        if (p_aky_uart_1_buf[3] == 0x5A)
                        {
                            Task1_BP.e_aky_task_1_stat = TASK_1_STAT_FAST_TEST;
                        }
                        if (p_aky_uart_1_buf[3] == 0x51)
                        {
                            Task1_BP.transfer_voice = p_aky_uart_1_buf[4];
                            Task1_BP.transfer_uint = p_aky_uart_1_buf[5];
                            Task1_BP.transfer_year = p_aky_uart_1_buf[6];
                            Task1_BP.transfer_month = p_aky_uart_1_buf[7];
                            Task1_BP.transfer_day = p_aky_uart_1_buf[8];
                            Task1_BP.transfer_hour = p_aky_uart_1_buf[9];
                            Task1_BP.transfer_min = p_aky_uart_1_buf[10];
                            Task1_BP.transfer_secon = p_aky_uart_1_buf[11];
                            Task1_BP.e_aky_task_1_stat = TASK_1_STAT_FREE;
                        }
                        if (p_aky_uart_1_buf[3] == 0x58)
                        {
                            Task1_BP.e_aky_task_1_stat = TASK_1_STAT_DOWN_CHANGE;
                        }
                        // if(Task1_BP.e_aky_task_1_stat == TASK_1_STAT_FREE)
                        {
                            if (p_aky_uart_1_buf[3] == 0xC1)
                            {
                                Task1_BP.e_aky_task_1_stat = TASK_1_STAT_ZERO;
                                // flag_aky_uart_1_judge = 1;
    
                            }
                            
                        }
                        // if(Task1_BP.e_aky_task_1_stat == TASK_1_STAT_ZERO)
                        {
                            if (p_aky_uart_1_buf[3] == 0xC2)
                            {
                                Task1_BP.e_aky_task_1_stat = TASK_1_STAT_TESTING;
						    	// flag_aky_uart_1_judge = 2;
                            }
                            
                        }
                        // if(Task1_BP.e_aky_task_1_stat == TASK_1_STAT_TESTING)
                        {
                            if (p_aky_uart_1_buf[3] == 0xC3)
                            {
                                Task1_BP.pres_mmg = p_aky_uart_1_buf[4] << 8 | p_aky_uart_1_buf[5];
                            }
                            if (p_aky_uart_1_buf[3] == 0xC4)
                            {
                                Task1_BP.result = p_aky_uart_1_buf[4];
                                Task1_BP.pres_sys = p_aky_uart_1_buf[5];
                                Task1_BP.pres_dia = p_aky_uart_1_buf[6];
                                Task1_BP.pulse = p_aky_uart_1_buf[7];
                                Task1_BP.PAD = p_aky_uart_1_buf[8];

                                if (Task1_BP.result == 1)
                                {
                                    Task1_BP.e_aky_task_1_stat = TASK_1_STAT_TEST_SUCC;
                                }
                                else if(Task1_BP.result == 0)
                                {
                                    Task1_BP.e_aky_task_1_stat = TASK_1_STAT_TEST_FAIL;
                                    Task1_BP.ERRORCode = p_aky_uart_1_buf[5];

                                }
                            }
                        }
                        // if(Task1_BP.e_aky_task_1_stat == TASK_1_STAT_TEST_SUCC || Task1_BP.e_aky_task_1_stat == TASK_1_STAT_TEST_FAIL)
                        {
                            if (p_aky_uart_1_buf[3] == 0xCF)
                            {
                                Task1_BP.e_aky_task_1_stat = TASK_1_STAT_FREE;
                            }
                        }
                        // if(Task1_BP.e_aky_task_1_stat == TASK_1_STAT_PER_READY )
                        {
                            if (p_aky_uart_1_buf[3] == 0xC5)
                            {
                                Task1_BP.PER_Alltime = Task1_BP.result = p_aky_uart_1_buf[4];
                                Task1_BP.PER_Steptime = Task1_BP.result = p_aky_uart_1_buf[4];
                            }
                            if (p_aky_uart_1_buf[3] == 0xC6)
                            {
                                Task1_BP.e_aky_task_1_stat = TASK_1_STAT_PER_WAIT;
                            }
                        }
                        // if(Task1_BP.e_aky_task_1_stat == TASK_1_STAT_PER_ING)
                        {
                            if (p_aky_uart_1_buf[3] == 0xC7)
                            {
                                Task1_BP.pres_mmg = p_aky_uart_1_buf[4] << 8 | p_aky_uart_1_buf[5];
                                Task1_BP.PER_Alltime = p_aky_uart_1_buf[6];
                                Task1_BP.PER_Steptime = (p_aky_uart_1_buf[6] * 60) + p_aky_uart_1_buf[7];
                                Task1_BP.PER_PumpStatus = p_aky_uart_1_buf[10];
                            }
                            else if (p_aky_uart_1_buf[3] == 0xC8)
                            {
                                Task1_BP.ERRORCode = p_aky_uart_1_buf[4];
                                Task1_BP.e_aky_task_1_stat = TASK_1_STAT_PER_FAIL;
                            }
                            else if (p_aky_uart_1_buf[3] == 0xC9)
                            {
                                Task1_BP.e_aky_task_1_stat = TASK_1_STAT_PER_WAIT;
                            }
                        }
                        // if(Task1_BP.e_aky_task_1_stat == TASK_1_STAT_MEM_GAIN)
                        {
                            if (p_aky_uart_1_buf[3] == 0xD1)
                            {
                                Task1_BP.MEM_INDEX = p_aky_uart_1_buf[4];
                                Task1_BP.MEM_pres_sys = p_aky_uart_1_buf[5];
                                Task1_BP.MEM_pres_dia = p_aky_uart_1_buf[6];
                                Task1_BP.MEM_pulse = p_aky_uart_1_buf[7];
                                Task1_BP.MEM_PAD = p_aky_uart_1_buf[8];
                                Task1_BP.Mem_year = p_aky_uart_1_buf[9];
                                Task1_BP.Mem_month = p_aky_uart_1_buf[10];
                                Task1_BP.Mem_day = p_aky_uart_1_buf[11];
                                Task1_BP.Mem_hour = p_aky_uart_1_buf[12];
                                Task1_BP.Mem_min = p_aky_uart_1_buf[13];
                                Task1_BP.Mem_secon = p_aky_uart_1_buf[14];

                                if(MEM_FIRST_GAIN_FLAGE_1 == 1)
                                {
                                    MEM_FIRST_GAIN_FLAGE_1 = 0;
                                    MEM_FIRST_GAIN_VALUE_1 = Task1_BP.MEM_INDEX;
                                }
                                
                                if(MEM_FIRST_GAIN_VALUE_1 == 0)
                                {
                                    Task1_BP.e_aky_task_1_stat = TASK_1_STAT_MEM_GAIN_OVER;
                                }
                                else if(MEM_FIRST_GAIN_VALUE_1 != 0)
                                {
                                    MEM_FIRST_GAIN_VALUE_1--;
                                    aky_task_1_send_cmd(TASK1_MEM_GAIN_Command);
                                }

                            }
                        }
                        // if(Task1_BP.e_aky_task_1_stat == TASK_1_STAT_MEM_DELEAT)
                        {
                            if (p_aky_uart_1_buf[3] == 0xD3)
                            {
                                Task1_BP.e_aky_task_1_stat = TASK_1_STAT_MEM_DELEAT_OVER;
                            }
                        }
                        // if(Task1_BP.e_aky_task_1_stat == TASK_1_STAT_C || Task1_BP.e_aky_task_1_stat == TASK_1_STAT_CAL)
                        {
                            if (p_aky_uart_1_buf[3] == 0x53)
                            {
                                Task1_BP.version_mon = p_aky_uart_1_buf[4];
                                Task1_BP.version_date = p_aky_uart_1_buf[5];
                            }
                            if(p_aky_uart_1_buf[3] == 0x54)
                            {
                                Task1_BP.pres_mmg = p_aky_uart_1_buf[4] << 8 | p_aky_uart_1_buf[5];
                            }
                            if(p_aky_uart_1_buf[3] == 0x55)
                            {
                                Task1_BP.Lefttestnumber = p_aky_uart_1_buf[4] << 8 | p_aky_uart_1_buf[5];
                                Task1_BP.e_aky_task_1_stat = TASK_1_STAT_LIFETEST;
                            }
                            if(p_aky_uart_1_buf[3] == 0x56)
                            {
                                Task1_BP.cal_succ_code = p_aky_uart_1_buf[4];
                                if (Task1_BP.cal_succ_code == 1)
                                {
                                    Task1_BP.e_aky_task_1_stat = TASK_1_STAT_CAL_SUCC;
                                }
                                else
                                {
                                    Task1_BP.e_aky_task_1_stat = TASK_1_STAT_CAL_ERR;
                                }
                            }
                        }
                        // if(Task1_BP.e_aky_task_1_stat == TASK_1_STAT_LIFETEST)
                        {
                            if(p_aky_uart_1_buf[3] == 0x55)
                            {
                                Task1_BP.Lefttestnumber = p_aky_uart_1_buf[4] << 8 | p_aky_uart_1_buf[5];
                            }
                        }
                        // if(Task1_BP.e_aky_task_1_stat == TASK_1_STAT_WRITE_MEM)
                        {
                            if(p_aky_uart_1_buf[3] == 0x52)
                            {
                                Task1_BP.WriteMem_time = p_aky_uart_1_buf[4];
                                Task1_BP.WriteMem_ok = p_aky_uart_1_buf[5];
                            }
                        }

                    }
                }
            }
        }
        
        aky_uart_1_read_cnt = 0;
        flag_aky_task_1_decode = 0;
    }
    //
}
void clr_task_1_data(void)
{
    memset(&Task1_BP,0x00,sizeof(stru_aky_task_1_t));
}
void aky_task_1_send_cmd(e_aky_Mcu_task1contrl cmd)
{
    uint8_t datalen;
    BLEUart1Data[0] = 0xA5;
    BLEUart1Data[1] = 0xA5;
    if (cmd == TASK1_BP_READY_Command)
    {
        datalen = 2;
        BLEUart1Data[2] = datalen;
        BLEUart1Data[3] = TASK1_BP_READY_Command;
    }
    if (cmd == TASK1_BP_START_Command)
    {
        datalen = 2;
        BLEUart1Data[2] = datalen;
        BLEUart1Data[3] = TASK1_BP_START_Command;
    }
    if (cmd == TASK1_BP_STOP_Command)   //退出血压模式或者预适应模式
    {
        datalen = 2;
        BLEUart1Data[2] = datalen;
        BLEUart1Data[3] = TASK1_BP_STOP_Command;
        Task1_BP.e_aky_task_1_stat = TASK_1_STAT_FREE;
        clr_task_1_data();
    }
    if (cmd == TASK1_PER_PREPARE_Command)
    {
        datalen = 6;
        BLEUart1Data[2] = datalen;
        BLEUart1Data[3] = TASK1_PER_PREPARE_Command;
        BLEUart1Data[4] = Task1_BP.PER_Mode;
        BLEUart1Data[5] = Task1_BP.pre_hold_time;
        BLEUart1Data[6] = (Task0_BP.pre_keep_pres & 0xff00) >> 8;
        BLEUart1Data[7] = Task0_BP.pre_keep_pres & 0x00ff;
    }
    if (cmd == TASK1_PER_UP)
    {
        datalen = 4;
        BLEUart1Data[2] = datalen;
        BLEUart1Data[3] = TASK1_PER_UP;
        BLEUart1Data[4] = (Task1_BP.pre_set & 0xff00) >> 8;
        BLEUart1Data[5] = Task1_BP.pre_set & 0x00ff;
    }
    if (cmd == TASK1_PER_DOWN)
    {
        datalen = 4;
        BLEUart1Data[2] = datalen;
        BLEUart1Data[3] = TASK1_PER_DOWN;
        BLEUart1Data[4] = (Task1_BP.pre_set & 0xff00) >> 8;
        BLEUart1Data[5] = Task1_BP.pre_set & 0x00ff;
    }
    if (cmd == TASK1_TIME_ADJUST)
    {
        datalen = 3;
        BLEUart1Data[2] = datalen;
        BLEUart1Data[3] = TASK1_TIME_ADJUST;
        BLEUart1Data[4] = Task1_BP.pre_hold_time;
    }

    if (cmd == TASK1_PER_START_Command)
    {
        datalen = 2;
        BLEUart1Data[2] = datalen;
        BLEUart1Data[3] = TASK1_PER_START_Command;
        Task1_BP.e_aky_task_1_stat = TASK_1_STAT_PER_ING;
    }
    if (cmd == TASK1_MEM_GAIN_Command)
    {
        datalen = 2;
        BLEUart1Data[2] = datalen;
        BLEUart1Data[3] = TASK1_MEM_GAIN_Command;
        Task1_BP.e_aky_task_1_stat = TASK_1_STAT_MEM_GAIN;
        MEM_FIRST_GAIN_FLAGE_1 = 1;
        MEM_FIRST_GAIN_VALUE_1 = 0;
    }
    if (cmd == TASK1_MEM_DELEAT_Command)
    {
        datalen = 2;
        BLEUart1Data[2] = datalen;
        BLEUart1Data[3] = TASK1_MEM_DELEAT_Command;
        Task1_BP.e_aky_task_1_stat = TASK_1_STAT_MEM_DELEAT;
    }
    if (cmd == TASK1_BP_DOWN_Command)
    {
        datalen = 2;
        BLEUart1Data[2] = datalen;
        BLEUart1Data[3] = TASK1_BP_DOWN_Command;
        Task1_BP.e_aky_task_1_stat = TASK_1_STAT_STOPMODE;
    }
    if (cmd == TASK1_C_Command)
    {
        datalen = 2;
        BLEUart1Data[2] = datalen;
        BLEUart1Data[3] = TASK1_C_Command;
        Task1_BP.e_aky_task_1_stat = TASK_1_STAT_C;
    }
    if (cmd == TASK1_CAL_Command)
    {
        datalen = 2;
        BLEUart1Data[2] = datalen;
        BLEUart1Data[3] = TASK1_CAL_Command;
        Task1_BP.e_aky_task_1_stat = TASK_1_STAT_CAL;
    }
    if (cmd == TASK1_BP_CALOK_Command)
    {
        datalen = 2;
        BLEUart1Data[2] = datalen;
        BLEUart1Data[3] = TASK1_BP_CALOK_Command;
    }
    if (cmd == TASK1_LifeTest_Command)
    {
        datalen = 2;
        BLEUart1Data[2] = datalen;
        BLEUart1Data[3] = TASK1_LifeTest_Command;
    }
    if (cmd == TASK1_MEM_WRITE_Command)
    {
        datalen = 2;
        BLEUart1Data[2] = datalen;
        BLEUart1Data[3] = TASK1_MEM_WRITE_Command;
    }
    if (cmd == TASK1_4G_TIME_Command)
    {
        datalen = 2;
        BLEUart1Data[2] = datalen;
        BLEUart1Data[3] = TASK1_4G_TIME_Command;
    }
    if (cmd == TASK1_ENTER_EMC)
    {
        datalen = 2;
        BLEUart1Data[2] = datalen;
        BLEUart1Data[3] = TASK1_ENTER_EMC;
    }
    if (cmd == TASK1_get_TIME_Command)
    {
        datalen = 2;
        BLEUart1Data[2] = datalen;
        BLEUart1Data[3] = TASK1_get_TIME_Command;
    }
    if (cmd == TASK1_FAST_TEST_MODE)
    {
        datalen = 2;
        BLEUart1Data[2] = datalen;
        BLEUart1Data[3] = TASK1_FAST_TEST_MODE;
    }    
    if (cmd == TASK1_BP_NEXT_STEP)
    {
        datalen = 3;
        BLEUart1Data[2] = datalen;
        BLEUart1Data[3] = TASK1_BP_NEXT_STEP;
        BLEUart1Data[4] = Task1_BP.step_code;
    }

    BLEUart1Data[datalen + 2] = Cal_CRC(datalen);
    BLEUart1Data[datalen + 3] = 0x5A; 
    BLEUart1Data[datalen + 4] = 0x5A;

    Send_BLe_data(datalen + 5);
}

uint8_t Cal_CRC(uint8_t len)
{
    uint8_t chensum, i;
    chensum = 0;
    for (i = 3; i < len + 2; i++)
    {
        chensum += BLEUart1Data[i];
    }
    return chensum;
}

void Send_BLe_data(uint8_t secdlen)
{
    uint8_t i;
    #if (AKY_PRINTF_TO_UART3 == 1)
    printf("tx1 Send\n");
    for (i = 0; i < secdlen; i++)
    {
        aky_uart_1_send_byte(BLEUart1Data[i]);
        printf("%02x ", BLEUart1Data[i]);
    }
    printf("\n");

    #else
    for (i = 0; i < secdlen; i++)
    {
        aky_uart_1_send_byte(BLEUart1Data[i]);
    }
    #endif
}

void aky_task_1(void) // 任务入口
{
    Timer1Callback();
    aky_task_1_uart_decode();
    /*此处后续添加未接收到回复值重发机制*/
}



static void aky_uart_1_init(void)
{
    UART_InitStructure UART_initStruct;
    GPIO_Init(GPION, PIN5, 0, 1, 0, 0);

    PORT_Init(PORTB, PIN0, PORTB_PIN0_UART1_TX, 0); // GPIOx.x配置为UART引脚
    PORT_Init(PORTN, PIN5, PORTN_PIN5_UART1_RX, 1); // GPIOx.x配置为UART引脚

    UART_initStruct.Baudrate = 115200; // 波特率
    UART_initStruct.DataBits = UART_DATA_8BIT;
    UART_initStruct.Parity = UART_PARITY_NONE;
    UART_initStruct.StopBits = UART_STOP_1BIT;
    UART_initStruct.RXThreshold = 0;
    UART_initStruct.RXThresholdIEn = 1;
    UART_initStruct.TXThreshold = 3;
    UART_initStruct.TXThresholdIEn = 0;
    UART_initStruct.TimeoutTime = 10;
    UART_initStruct.TimeoutIEn = 0;
    UART_Init(UART1, &UART_initStruct);
    UART_Open(UART1);
}

// 发送一个字节
static void aky_uart_1_send_byte(uint8_t data)
{
    uint32_t time_out = 0;
    UART_WriteByte(UART1, data);
    while (UART_IsTXBusy(UART1))
    {
        time_out++;
        __NOP();
        if (time_out > 666666)
        {
            break;
        }
    }
}




void aky_task_1_init(void)
{
    aky_uart_1_init();
}

